package irri.soils.ssnm;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

public class ActivityMain3 extends SSNM implements View.OnClickListener{
	
	private int option = 0;
	private View button1;
	private RadioButton radio1;
	private RadioButton radio2;
	private Bundle b;
	private Intent intent;
	private TextView question;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main3);
        setCustomTitle();
        b = this.getIntent().getExtras();
        radio1 = (RadioButton) findViewById(R.id.radio1);
        radio1.setOnClickListener(this);
        radio2 = (RadioButton) findViewById(R.id.radio2);
        radio2.setOnClickListener(this);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);
        button1.setEnabled(false);
        question = (TextView) findViewById(R.id.question);
        question.setText(get("reminder0"));
        radio1.setText(get("note0"));
        radio2.setText(get("note2"));
        SQLiteDatabase myDB= null;
        boolean hasEntry = true;
        try {
            myDB = this.openOrCreateDatabase("SSNM2", MODE_WORLD_WRITEABLE, null);
            myDB.execSQL("create table IF NOT EXISTS phil (ref_id INTEGER PRIMARY KEY AUTOINCREMENT, "
        			+"session_data TEXT NULL, "
        			+"accessed TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP);");
            
            Cursor c = myDB.rawQuery("SELECT count(*) FROM phil", null);
            if (c != null) {
                c.moveToFirst();
                if(c.getInt(0) == 0) hasEntry = false;
                System.out.println("Count->"+c.getInt(0));
                c.close();
            }
        }
        catch(Exception e) {
        } finally {
           if(myDB!=null) myDB.close();
        }
        TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if(!hasEntry||!checkInternetConnection()||telMgr.getSimState()==TelephonyManager.SIM_STATE_ABSENT){
    	   intent = new Intent(ActivityMain3.this, ActivityB.class);
    	   intent.putExtras(b);
    	   startActivity(intent);
           finish();
        }
    }
    
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(radio1.isChecked()) radio1.performClick();
		if(radio2.isChecked()) radio2.performClick();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.radio1: option = 1; button1.setEnabled(true); break;
		case R.id.radio2: option = 2; button1.setEnabled(true); break;
		case R.id.button1:
			Intent intent = new Intent(ActivityMain3.this, option==1 ? 
					ActivityMain4.class : ActivityB.class);
			intent.putExtras(b);
			startActivity(intent);
			finish();
			break;
		}
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(ActivityMain3.this, ActivityMain.class);
		intent.putExtras(b);
		startActivity(intent);
		finish();
	}
}